<?php

class SiteController extends FrontController
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		//Hot
		$criteria=new CDbCriteria();
		$criteria->condition = "t.active=1 AND t.hot=1";
		$criteria->limit = 7;
		$criteria->order='t.order DESC';
		
		$dataProvider=new CActiveDataProvider('Home', array(
				'criteria'=>$criteria,
				'pagination'=>false,
		));
		//Tivi
		$TVcriteria=new CDbCriteria();
		$TVcriteria->condition = "t.hot=1";
		$TVcriteria->limit = 5;
		$TVcriteria->order='t.order ASC';
		$TVdataProvider=new CActiveDataProvider('Tv', array(
    		'criteria'=>$TVcriteria,
			'pagination'=>false,
		));
		//Film
		$Fcriteria=new CDbCriteria();
		$Fcriteria->condition = "t.global_hot=1";
		$Fcriteria->limit = 5;
		$Fcriteria->order='t.global_order DESC';
		$FdataProvider=new CActiveDataProvider('Film', array(
    		'criteria'=>$Fcriteria,
			'pagination'=>false,
		));
		//Clip
		$Ccriteria=new CDbCriteria();
		$Ccriteria->condition = "t.global_hot=1";
		$Ccriteria->limit = 5;
		$Ccriteria->order='t.global_order DESC';
		$CdataProvider=new CActiveDataProvider('Clip', array(
    		'criteria'=>$Ccriteria,
			'pagination'=>false,
		));
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
			'TVdataProvider'=>$TVdataProvider,
			'FdataProvider'=>$FdataProvider,
			'CdataProvider'=>$CdataProvider,
		));
	}
	//JSON
	public function actionHothome()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		//Hot
		if(isset($_POST['user']) && isset($_POST['passwd'])){
			if(($_POST['user'] === 'mbapp') && ($_POST['passwd'] === '6EVAje6U')){
				$criteria=new CDbCriteria();
				$criteria->condition = "t.active=1 AND t.hot=1";
				$criteria->limit = 10;
				$criteria->order='t.order DESC';
			
				$dataProvider=new CActiveDataProvider('Home', array(
						'criteria'=>$criteria,
						'pagination'=>false,
				));

				header('Content-type: application/json');

				$jsondata = array('hothome' => $dataProvider->getData());
				echo CJSON::encode($jsondata);
				Yii::app()->end();
			}
		}
	}
	public function actionFilmhome()
	{
		if(isset($_POST['user']) && isset($_POST['passwd'])){
			if(($_POST['user'] === 'mbapp') && ($_POST['passwd'] === '6EVAje6U')){
				// New
				$criteriaNew = new CDbCriteria ();
				$criteriaNew->condition = "t.active=1 AND t.global_hot=1";
				$criteriaNew->limit = '5';
				$criteriaNew->order = 't.id DESC';
				$FdataProvider = new CActiveDataProvider ( 'Film', array (
						'criteria' => $criteriaNew,
						'pagination' => false
				) );
				header('Content-type: application/json');

				$jsondata = array('filmhome' =>$FdataProvider->getData());
				echo CJSON::encode($jsondata);
				Yii::app()->end();
			}
		}
	}
	public function actionCliphome()
	{
		if(isset($_POST['user']) && isset($_POST['passwd'])){
			if(($_POST['user'] === 'mbapp') && ($_POST['passwd'] === '6EVAje6U')){
				$Ccriteria=new CDbCriteria();
				$Ccriteria->condition = "t.active=1 AND t.global_hot=1";
				$Ccriteria->limit = '5';
				$Ccriteria->order='t.order DESC';
				$CdataProvider=new CActiveDataProvider('Clip', array(
						'criteria'=>$Ccriteria,
						'pagination'=>false
				));
				header('Content-type: application/json');
	
				$jsondata = array('cliphome' =>$CdataProvider->getData());
				echo CJSON::encode($jsondata);
				Yii::app()->end();
			}
		}
	}
	public function actionTvhome()
	{
		if(isset($_POST['user']) && isset($_POST['passwd'])){
			if(($_POST['user'] === 'mbapp') && ($_POST['passwd'] === '6EVAje6U')){
				$TVcriteria=new CDbCriteria();
				$TVcriteria->condition = "t.active=1 AND t.hot=1";
				$TVcriteria->limit = '5';
				$TVcriteria->order='t.order DESC';
				$TVdataProvider=new CActiveDataProvider('Tv', array(
						'criteria'=>$TVcriteria,
						'pagination'=>false
				));
				header('Content-type: application/json');
				$jsondata = array('tvhome' =>$TVdataProvider->getData());
				echo CJSON::encode($jsondata);
				Yii::app()->end();
			}
		}
	}
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}
	/**
	 * Displays the support page
	 */
	public function actionSupport()
	{
		$this->render('support');
	}
	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new FrontLoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['FrontLoginForm']))
		{
			$model->attributes=$_POST['FrontLoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login()){
				$this->redirect(Yii::app()->user->returnUrl);
			}
		}
		// display the login form
		$package=new Package();
		$package=Package::model()->findByAttributes(array('type'=>'4','active'=>'1'));//SMS 6576 5000VNĐ
		//$package=Package::model()->findByAttributes(array('type'=>'3','active'=>'1'));//SMS 6776 15000VNĐ
		$this->render('login',array('model'=>$model,'package'=>$package));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}

    public function actionMoveimage(){
        $Film = Clip::model()->findAll();
        foreach($Film as $film){
            $image =$film->image;
            if(strpos($image,'http') !== 0){
                $model = Clip::model()->findByPk($film->id);
                $model->image = 'http://210.211.99.203/'.$image;
                $model->save();
                echo $model->image;
                echo '<br>';
            }
        }
    }
}